<?php

class SellController extends Zend_Controller_Action
{

    public function init()
    {
        $authData = Zend_Auth::getInstance()->getStorage()->read();
		if (is_array($authData) and $authData['identity'] != 'anon'){
		    if (isset($authData['userType']) and strtolower($authData['userType']) == 'agnt') {
		        $_SESSION['userType'] = strtolower($authData['userType']);
		    } 
			$_SESSION['agent_reg_id'] = strtolower($authData['identity']);
        }
    }

    public function indexAction()
    {
        if (isset($_SESSION['userType'])) {
    	$this->view->assign('userType',$_SESSION['userType']);
        }
        if (isset($_SESSION['agent_reg_id'])) {
    	$this->view->assign('user',$_SESSION['agent_reg_id']);
        }
    }
    
    public function registerAction()
    {
        $this->_helper->viewRenderer->setNoRender(TRUE);
        $this->_helper->layout()->disableLayout();
        $request = $this->getRequest();
        $params = array_diff($request->getParams(), $request->getUserParams());
        $estate = array();
        foreach ($params as $colName => $value) {
            $value = is_array($value)?$value:htmlentities(trim($value));
            $estate[$colName] = $value;
        }
        if ('sqft' != $estate['area_unit']) {
            $estate['size'] = $this->_helper->UnitConverter($estate['size'],$estate['area_unit']);
            $unit = $this->_helper->UnitConverter(1,'sqft',$estate['area_unit']);
            $estate['rate_per_unit'] = $estate['rate_per_unit']*$unit;
        }
        $estate['input_unit'] = $estate['area_unit'];
        $property = new Reale_Model_Estate_Property($estate);
        
        
        $oper = $request->getParam('oper');
        switch ($oper) {
            case 'add':
            case 'mod':
                if ($property->save()) {
                    echo 'Submit successful.';
                    return;
                }
                
            break;
            case 'del':
            break;
            default:
                throw new Zend_Exception('"'.$oper.'" is unknown operation.', Zend_Log::ERR);
            break;
        }
        throw new Zend_Exception('Operation on property not successfull.', Zend_Log::ERR);
    }
    
        
}

